using System;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControlMap : MonoBehaviour
{
    private int maxX, maxY;
    private Waypoint nextWaypoint;
    private string nextLevel;
    private Waypoint[,] waypoints;
    private int x, y;

    private void Start()
    {
        GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
        int tempX = 0, tempY = 0;

        foreach (GameObject go in waypoints)
        {
            string[] s = go.name.Split(',');
            int temp1 = Convert.ToInt32(s[0]);
            int temp2 = Convert.ToInt32(s[1]);

            if (temp1 > tempX) tempX = temp1;
            if (temp2 > tempY) tempY = temp2;
        }

        this.maxX = tempX;
        this.maxY = tempY;
        this.waypoints = new Waypoint[this.maxX + 1, this.maxY + 1];

        foreach (GameObject go in waypoints)
        {
            string[] s = go.name.Split(',');
            tempX = Convert.ToInt32(s[0]);
            tempY = Convert.ToInt32(s[1]);

            this.waypoints[tempX, tempY] = new Waypoint(go.transform.position, s[2]);
        }

        EventManager.OnInput += EventManagerOnInput;
    }

    private void EventManagerOnInput(object sender, List<InputEventArgs> e)
    {
        if (this.nextWaypoint != null) return;

        foreach (InputEventArgs input in e)
        {
            switch (input.Device)
            {
                case InputDevice.Keyboard:
                    switch (input.Input)
                    {
                        case "W":
                            if (this.y < this.maxY && this.waypoints[this.x, this.y + 1] != null)
                                this.y++;
                            break;
                        case "S":
                            if (this.y > 0 && this.waypoints[this.x, this.y - 1] != null)
                                this.y--;
                            break;
                        case "D":
                            if (this.x < this.maxX && this.waypoints[this.x + 1, this.y] != null)
                                this.x++;
                            break;
                        case "A":
                            if (this.x > 0 && this.waypoints[this.x - 1, this.y] != null)
                                this.x--;
                            break;
                    }
                    this.nextWaypoint = this.waypoints[this.x, this.y];
                    break;
                case InputDevice.XboxController:
                    if (input.Type == InputType.DPad)
                    {
                        switch (input.Input)
                        {
                            case "v1":
                                if (this.y < this.maxY && this.waypoints[this.x, this.y + 1] != null)
                                    this.y++;
                                break;
                            case "v-1":
                                if (this.y > 0 && this.waypoints[this.x, this.y - 1] != null)
                                    this.y--;
                                break;
                            case "h1":
                                if (this.x < this.maxX && this.waypoints[this.x + 1, this.y] != null)
                                    this.x++;
                                break;
                            case "h-1":
                                if (this.x > 0 && this.waypoints[this.x - 1, this.y] != null)
                                    this.x--;
                                break;
                        }
                        this.nextWaypoint = this.waypoints[this.x, this.y];
                    }
                    break;
            }
        }
    }

    private void Update()
    {
        if (this.nextWaypoint != null)
        {
            transform.position = Vector3.MoveTowards(transform.position, this.waypoints[this.x, this.y].Coordinates,
                                                     2 * Time.deltaTime);
            nextLevel = "delete";
            if (Vector3.Distance(transform.position, this.nextWaypoint.Coordinates) <= 0.1f)
            {
                nextLevel = this.nextWaypoint.Name;
                this.nextWaypoint = null;
            }
        }

        if (!string.IsNullOrEmpty(nextLevel) && nextLevel != "leer")
        {
            EventManager.NextLevel = nextLevel;
            nextLevel = "";
        }
    }

    private void OnDestroy()
    {
        EventManager.OnInput -= EventManagerOnInput;
    }
}